package net.aaa.hotel.controller;

import com.alibaba.fastjson.JSON;
import net.aaa.hotel.dao.DeptDao;
import net.aaa.hotel.dao.HRoleDao;
import net.aaa.hotel.dao.YGDao;
import net.aaa.hotel.dao.impl.DeptDaoImpl;
import net.aaa.hotel.dao.impl.HRoleDaoImpl;
import net.aaa.hotel.dao.impl.IYGDaoImpl;
import net.aaa.hotel.entity.HYg;
import net.aaa.hotel.entity.PageInfo;
import net.aaa.hotel.util.LayuiTable;
import net.aaa.hotel.util.TransBean;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/yg")
public class YGController extends HttpServlet {
    private YGDao dao = new IYGDaoImpl();
    private HRoleDao daoR = new HRoleDaoImpl();
    private DeptDao daoD = new DeptDaoImpl();

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setContentType("text/html; charset=UTF-8");
        String method = req.getParameter("method");
        if (method == null) {
            List<Map> map = dao.queryAll();
            Map m = new HashMap();
            m.put("code", 0);
            m.put("msg", "");
            m.put("count", map.size());
            m.put("data", map);
            String string = JSON.toJSONStringWithDateFormat(m, "yyyy/MM/dd");
            PrintWriter out = resp.getWriter();
       //     System.out.println(string);
            out.write(string);
            out.flush();
            out.close();
        } else {
            switch (method) {
                case "add":
                    addYG(req, resp);
                    break;
                case "queryOne":
                    queryOne(req, resp);
                    break;
                case "del":
                    delYG(req, resp);
                    break;
                case "queryR":
                    queryR(req, resp);
                    break;
                case "queryD":
                    queryD(req, resp);
                    break;
                case "checkLogin":
                    checkLogin(req, resp);
                    break;
                case "exit":
                    exit(req, resp);
                    break;
                case "showTable":
                    showTable(req, resp);
                    break;

            }
        }
    }

    private void showTable(HttpServletRequest req, HttpServletResponse resp) throws IOException {
    //    System.out.println("showTable");
        String cu = req.getParameter("curr");
        String nu = req.getParameter("nums");
        String condition = req.getParameter("condition");

//        System.out.println("condition+++++++"+condition);
//        System.out.println("curr+++++"+cu);
//        System.out.println("nums+++++"+nu);

        int curr = Integer.parseInt(cu);
        int nums = Integer.parseInt(nu);
        int count = dao.getCount(condition);
        List<Map> allByPage = dao.getAllByPage(curr, nums,condition);

//        System.out.println("curr+++++"+curr);
//        System.out.println("nums+++++"+nums);
//        System.out.println("allBypage++++++"+allByPage);

        LayuiTable layuiTable = new LayuiTable(count, allByPage);
        String string = JSON.toJSONStringWithDateFormat(layuiTable, "yyyy/MM/dd");
        PrintWriter out = resp.getWriter();
//        String s=JSON.toJSONStringWithDateFormat(string,"yyyy-MM-dd");    //  将数据转换成时间格式  年月日 yyyy-MM-dd
        out.print(string);
        out.flush();
        out.close();
    }

    private void queryD(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Map> dept = daoD.findAll();
        String string = JSON.toJSONString(dept);
        PrintWriter out = resp.getWriter();
        out.print(string);
        out.flush();
        out.close();
    }

    private void queryR(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        List<Map> role = daoR.findAll();
        String string = JSON.toJSONString(role);
        PrintWriter out = resp.getWriter();
        out.print(string);
        out.flush();
        out.close();
    }

    private void delYG(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String gid = req.getParameter("gid");
        int i = dao.delYG(gid);
        resp.sendRedirect("yg/show.jsp");
    }

    private void queryOne(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("addname");
        List<Map> maps = dao.queryOne(name);
        PrintWriter w = resp.getWriter();
        if (maps.size() > 0) {

            w.print(1);

        } else {
            w.print(0);
        }
        w.flush();
        w.close();
    }

    private void addYG(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        Map<String, String[]> map = req.getParameterMap();
    //    System.out.println(map);
        HYg yg = new HYg();
        TransBean.populate(yg, map);
    //    System.out.println(yg.getStatus());
        int i = dao.addYg(yg);
        PrintWriter out = resp.getWriter();
        if (i > 0) {
            out.print(1);
        } else {
            out.print(0);
        }
        out.flush();
        out.close();
    }

    private void checkLogin(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        String name = req.getParameter("name");
        String password = req.getParameter("password");
        List<Map> maps = dao.findUserByNameAndPaddword(name, password);
        PrintWriter out = resp.getWriter();
        if (maps.size() > 0) {
            List<HYg> ulist = TransBean.populate(HYg.class, maps);
            ulist.forEach(System.out::println);
            HYg user = ulist.get(0);
            if (user.getStatus() > 0) {
                out.print("1");
            } else {
                HttpSession session = req.getSession();
                session.setAttribute("uname", name);
                session.setAttribute("id",maps.get(0).get("id"));
                session.setAttribute("urp",maps.get(0).get("imgpath"));
                session.setAttribute("pwd",maps.get(0).get("pwd"));
                session.setAttribute("rid",maps.get(0).get("jueseid"));
                out.print("0");
            }
        } else {

            out.println("-1");

        }
        out.flush();
        out.close();
    }

    private void exit(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        HttpSession session = req.getSession();
        session.invalidate();
        resp.sendRedirect("/hlogin.jsp");
    }
}
